﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChartTools
{
    /// <summary>
    /// 点状图
    /// </summary>
    public class Scatter
    {
        private string Title;
        private string XName;
        private string YName;
        private List<ScatterData> Datas;
        private string DataName1;
        private string DataName2;
        private List<Point> Points1;
        private List<Point> Points2;
        private string Function;
        private string ShowDivId;
        public Scatter(string pTitle, string pXname, string pYname, List<ScatterData> pDatas, List<Point> pPoints, string pFunction, string pShowDIV)
        {
            Title = pTitle;
            XName = pXname;
            YName = pYname;
            Datas = pDatas;
            Points1 = pPoints;
            Points2 = null;
            Function = pFunction;
            ShowDivId = pShowDIV;
            DataName1 = "";
            DataName2 = "";
        }
        public Scatter(string pTitle, string pXname, string pYname, List<ScatterData> pDatas, string pDataName1, string pDataName2, List<Point> pPoints1, List<Point> pPoints2, string pFunction, string pShowDIV)
        {
            Title = pTitle;
            XName = pXname;
            YName = pYname;
            Datas = pDatas;
            Points1 = pPoints1;
            Points2 = pPoints2;
            Function = pFunction;
            ShowDivId = pShowDIV;
            DataName1 = pDataName1;
            DataName2 = pDataName2;
        }
        private Scatter() { }
        public override string ToString()
        {
            double _maxY = 0;

            foreach (Point p in Points1)
            {
                if (p.Y > _maxY)
                {
                    _maxY = p.Y;
                }
            }
            if (Points2 != null)
            {
                foreach (Point p in Points2)
                {
                    if (p.Y > _maxY)
                    {
                        _maxY = p.Y;
                    }
                }
            }
            var _maxX = from data in Datas select data.Value;
            int _maxXValue = _maxX.Max();
            StringBuilder sb = new StringBuilder();
            sb.Append("<chart palette='2' caption='" + Title + "'  yAxisName='" + YName + "' xAxisName='" + XName + "' rotateLabels='0' showLegend='3' showLabels='1' xAxisMaxValue='" + _maxXValue + "' xAxisMinValue='0' yAxisMaxValue='" + _maxY + "' decimals='1'>");
            sb.Append("<categories verticalLineColor='666666' verticalLineThickness='1'>");
            foreach (ScatterData data in Datas)
            {
                sb.Append("<category label='" + data.Label + "' x='" + data.Value + "' showVerticalLine='1'/>");
            }
            sb.Append("</categories>");
            if (!string.IsNullOrEmpty(DataName1))
            {
                sb.Append("<dataSet seriesName='" + DataName1 + "' color='009900' anchorSides='3' anchorRadius='3' anchorBgColor='009900' anchorBorderColor='009900' >");
            }
            else
            {
                sb.Append("<dataSet seriesName='' color='009900' anchorSides='3' anchorRadius='3' anchorBgColor='009900' anchorBorderColor='009900' >");
            }
            foreach (Point data in Points1)
            {
                sb.Append("<set y='" + data.Y + "' x='" + data.X + "'/>");
            }
            sb.Append("</dataSet>");

            if (Points2 != null)
            {
                sb.Append("<dataSet seriesName='" + DataName2 + "' color='990000' anchorSides='4' anchorRadius='3' anchorBgColor='990000' anchorBorderColor='990000' >");
                foreach (Point data in Points2)
                {
                    sb.Append("<set y='" + data.Y + "' x='" + data.X + "'/>");
                }
                sb.Append("</dataSet>");
            }

            sb.Append("<hTrendlines>");
            var query = from point in Points1 select point.Y;
            double avg = query.Average();
            double avg2 = 0;
            if (Points2 != null)
            {
                var query2 = from point in Points2 select point.Y;
                avg2 = query.Average();
            }
            sb.Append("<line startValue='" + (avg + avg2) / 2 + "' displayValue='平均值' lineThickness='2' color='FF0000' valueOnRight='1' dashed='1' dashGap='5'/>");
            sb.Append("</hTrendlines>");
            sb.Append("</chart>");
            string datastring = sb.ToString();
            sb = new StringBuilder();
            sb.Append("<script type='text/javascript'>");
            sb.Append("function " + Function + "(){");
            sb.Append("var chart = new FusionCharts('/Content/FusionCharts/Scatter.swf', 'ChartId', '100%', '260', '0', '0');");
            sb.Append("chart.setXMLData(\"" + datastring + "\");");
            sb.Append("chart.render('" + ShowDivId + "');");
            sb.Append("}");
            sb.Append("</script>");
            return sb.ToString();
        }
    }

    public class ScatterData
    {
        public string Label { get; set; }

        public int Value { get; set; }
    }

    public class Point
    {
        public double X { get; set; }
        public double Y { get; set; }
    }
}
